﻿using UnityEngine;
using System.Collections;
using System;
using System.Windows.Forms;
using System.IO;
using UnityEngine.UI;
using LeanCloud;

public class AddHead : MonoBehaviour
{
    private Texture2D img = null;
    public Image image;
    
    public Sprite sprite;
    // Use this for initialization
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
    }
    /*void OnGUI(){
		if (GUI.Button (new Rect (0, 0, 100, 20), "选择文件")) {
	}*/
    public void addhead()
    {
        OpenFileDialog od = new OpenFileDialog();
        od.Title = "请选择头像图片";
        od.Multiselect = false;
        od.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";
        if (od.ShowDialog() == DialogResult.OK)
        {
            //Debug.Log(od.FileName);
            StartCoroutine(GetTexture("file://" + od.FileName));
        }
        /*if (img != null) {
			//GUI.DrawTexture(new Rect(0,20,img.width,img.height),img);
			image.sprite=sprite;
		}*/
    }
    IEnumerator GetTexture(string url)
    {
        WWW www = new WWW(url);
        yield return www;
        if (www.isDone && www.error == null)
        {
            img = www.texture;
            sprite = Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f));
            image.sprite = sprite;
            //Debug.Log(img.width+" "+img.height);
            byte[] date = img.EncodeToPNG();
        }
    }
    //上传图片方法
    public async System.Threading.Tasks.Task APPAsync() {
        
        AVFile file = new AVFile("screenshot.png", Path.Combine(UnityEngine.Application.persistentDataPath, "screenshot.PNG"));
        await file.SaveAsync();
        Debug.Log(file.ObjectId);

    }
    public void AAA() {
        APPAsync();
    }
}